package com.example.designpattern.singleExample;

import java.io.Serializable;

/**
 * @program: DesignPattern
 * @description: 双重检测锁创建单例bean
 * @author: zhangjt
 * @create: 2024-11-14 14:46
 **/
public class DulLazyCreateBean implements Serializable {
    // volatile 关键字可以确保实例变量的可见性，避免发生指令重排。
    private static volatile DulLazyCreateBean instance;

    private DulLazyCreateBean(){}

    public static DulLazyCreateBean getInstance(){
        if(instance == null){
            synchronized (DulLazyCreateBean.class){
                if(instance == null){
                    instance = new DulLazyCreateBean();
                }
            }
        }
        return instance;
    }
}
